﻿using System.Diagnostics;

namespace WebHome_API.Tools
{
    public class Ipv6
    {
        public static string getIpv6()
        {
            var arr = ExecuteInCmd("ipconfig").Split('\n');
            //临时 IPv6 地址. . . . . . . . . . : 240e:33e:942a: 2000:8d6c: 4c6a: 7ff1: 2cc3\r
            var cur = arr.FirstOrDefault(u => u.IndexOf("临时 IPv6 地址") != -1);
            var index = cur.IndexOf(":");
            var ipv6 = cur.Substring(index + 2, cur.Length - index - 3);
            return ipv6;
        }

        public static string ExecuteInCmd(string cmdline)
        {
            using (var process = new Process())
            {
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;

                process.Start();
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine(cmdline + "&exit");

                //获取cmd窗口的输出信息  
                string output = process.StandardOutput.ReadToEnd();

                process.WaitForExit();
                process.Close();

                return output;
            }
        }
    }
}
